package com.snatch.snatchmgr.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.snatchmgr.dto.ThirdMatchDto;
import com.snatch.snatchmgr.entity.db.ThirdLive;
import com.snatch.snatchmgr.service.AdminDataService;
import com.snatch.snatchmgr.service.SnatchService;
import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.vo.GameType;
import com.snatch.snatchmgr.vo.League;
import com.snatch.snatchmgr.vo.Match;
import com.snatch.snatchmgr.vo.SnatchGame;


@Controller
@RequestMapping("/data")
public class DataController {
	
	@Autowired
	SnatchService snatchService;
	
	@Autowired
	AdminDataService adminDataService;
	
	@RequestMapping("findthirdMatch")
	public String findthirdMatch(Model model,String webCode,String gameCode,String gameType,String leagueid,String matchid){
		if(null == webCode){
			webCode = SnatchConstant.THIRD_SYSTEM_HG_VALUE;
		}
		if(null == gameCode){
			gameCode = SnatchConstant.FOOTBALL;
		}
		if(null == gameType){
			gameType = SnatchConstant.SNATCH_TYPE_GQ;
		}
		List<SnatchGame> snatchGames = snatchService.findGames(webCode);
		List<League> leagues = adminDataService.findLeagues(webCode,gameCode,gameType);
		List<GameType> gameTypes = SystemData.findGameTypes();
		
		List<Match> matchs = new ArrayList<Match>();
		ThirdMatchDto thirdMatch = null;
		if(null != leagueid){
			matchs = adminDataService.findMatchs(webCode, gameCode,gameType, leagueid);
			if(null != matchid){
				thirdMatch = adminDataService.findThirdMatch(webCode, gameCode,gameType, leagueid, matchid);
			}
		}
		
		model.addAttribute("snatchGames",snatchGames);
		model.addAttribute("leagues",leagues);
		model.addAttribute("matchs",matchs);
		model.addAttribute("thirdMatch",thirdMatch);
		model.addAttribute("gameTypes",gameTypes);
		
		
		model.addAttribute("webCode", webCode);
		model.addAttribute("gameCode", gameCode);
		model.addAttribute("gameType", gameType);
		model.addAttribute("leagueid", leagueid);
		model.addAttribute("matchid", matchid);
		return "data/thirdMatch";
	}
	
	
	
	@RequestMapping("findthirdLive")
	public String findthirdLive(Model model,String webCode,String gameCode,String leagueid,String matchid){
		String gameType = SnatchConstant.SNATCH_TYPE_GQ;
		
		if(null == webCode){
			webCode = SnatchConstant.THIRD_SYSTEM_HG_VALUE;
		}
		if(null == gameCode){
			gameCode = SnatchConstant.FOOTBALL;
		}
		String livekey = SystemData.getLiveMatchMapKey(leagueid, matchid);
		List<SnatchGame> snatchGames = snatchService.findGames(webCode);
		List<League> leagues = adminDataService.findLeagues(webCode,gameCode,gameType);
		
		List<Match> matchs = new ArrayList<Match>();
		ThirdLive liveData = null;
		if(null != leagueid){
			matchs = adminDataService.findMatchs(webCode, gameCode,gameType, leagueid);
			if(null !=matchid){
				liveData = adminDataService.findLiveData(webCode,gameCode, livekey);
			}
		}
		
		model.addAttribute("snatchGames",snatchGames);
		model.addAttribute("leagues",leagues);
		model.addAttribute("matchs",matchs);
		model.addAttribute("liveData", liveData);
		
		
		model.addAttribute("webCode", webCode);
		model.addAttribute("gameCode", gameCode);
		model.addAttribute("gameType", gameType);
		model.addAttribute("leagueid", leagueid);
		model.addAttribute("matchid", matchid);
		
		
		return "data/thirdLive";
	}
	
	
	
	
}
